package com.wpp.register.loaderbalance;

import com.wpp.register.ServerInfo;
import com.wpp.register.loaderbalance.LoaderBalance;

import java.util.List;

/**
 * @Classname RandomLoaderBalance
 * @Description TODO
 * @Date 2020/12/7 23:44
 * @Created by wpp25
 */
public class RandomLoaderBalance implements LoaderBalance {
    @Override
    public ServerInfo balance(List<ServerInfo> serverInfoList) {
        int index = getServerNum(serverInfoList.size());
        System.out.println("当前服务列表： " + serverInfoList);
        ServerInfo serverInfo = serverInfoList.get(index);
        System.out.println("本次访问的服务是: "+ serverInfo);
        return serverInfo;
    }


    private static int getServerNum(int size) {
        return (int) (Math.random() * size);
    }

}
